package com.xh.bussiness.xkw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.dto.DictionaryBean;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.model.DictionaryRel;
import com.xh.bussiness.xkw.model.XkwSysDictionary;
import com.xh.bussiness.xkw.dao.XkwSysDictionaryMapper;
import com.xh.bussiness.xkw.model.XkwSysDictionaryRel;
import com.xh.bussiness.xkw.service.IXkwSysDictionaryRelService;
import com.xh.bussiness.xkw.service.IXkwSysDictionaryService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 学科网数据字典表 服务实现类
 * </p>
 *
 * @author Jqm
 * @since 2021-05-20
 */
@Service
public class XkwSysDictionaryServiceImpl extends BaseServiceImpl<XkwSysDictionaryMapper, XkwSysDictionary> implements IXkwSysDictionaryService {

    @Autowired
    IXkwSysDictionaryRelService dictionaryRelService;
    @Override
    public Map<String, List<XkwSysDictionary>> querySubject(){
        Map<String, List<XkwSysDictionary>> map = new HashMap<String, List<XkwSysDictionary>>();
        QueryWrapper<XkwSysDictionary> qw = new QueryWrapper<>();
        qw.eq("type_code","GRADE_RANGE");
        qw.eq("status",0);
        List<XkwSysDictionary> dictionaries = this.selectList(qw,XkwSysDictionary.class);
        for (XkwSysDictionary dictionary : dictionaries) {
            String code = dictionary.getCode();
            QueryWrapper<XkwSysDictionaryRel> qwRel = new QueryWrapper<>();
            qwRel.eq("parent_code",code);
            qwRel.eq("child_type_code","SUBJECT");
            List<XkwSysDictionaryRel> dictionaryRels = dictionaryRelService.queryByCondition(qwRel);
            List<String> clildCodes = dictionaryRels.stream().map(XkwSysDictionaryRel::getChildCode).collect(Collectors.toList());
            //取当前年级所有学科 和 用户学科权限的交集
            List<XkwSysDictionary> dictionaries1= new ArrayList<>();
            if(clildCodes.size() > 0){
                qw = new QueryWrapper<>();
                qw.eq("type_code","SUBJECT");
                qw.in("code",clildCodes);
                qw.orderByAsc("order_no");
                dictionaries1 = super.queryByCondition(qw);
            }
            map.put(code,dictionaries1);
        }
        return map;
    }
}
